package com.tools.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tools.common.enums.ResultCode;
import com.tools.common.result.Result;
import com.tools.pojo.user.dto.MenuDTO;
import com.tools.pojo.user.vo.MenuVO;
import com.tools.service.user.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@AllArgsConstructor
@Tag(name = "菜单模块",description = "菜单模块")
public class MenuController {

    private final MenuService menuService;


    @Operation(summary = "获取菜单树", description = "获取所有菜单的树形结构")
    @GetMapping("/tree")
    public Result<List<MenuVO>> getMenuTree() {
        List<MenuVO> tree = menuService.tree();

        return Result.success(tree);
    }

    @Operation(summary = "新增菜单", description = "新增菜单项")
    @PostMapping("/add")
    public Result<Boolean> addMenu(@RequestBody @Valid MenuDTO menuDTO) {
        return menuService.saveMenu(menuDTO) ?
                Result.success() : Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    @Operation(summary = "更新菜单", description = "更新菜单信息")
    @PostMapping("/update")
    public Result<Boolean> updateMenu(@RequestBody @Valid MenuDTO menuDTO) {
        return menuService.updateMenu(menuDTO) ?
                Result.success() : Result.fail(ResultCode.DATA_UPDATE_ERROR);
    }

    @Operation(summary = "删除菜单", description = "根据ID删除菜单")
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> removeMenu(@PathVariable Long id) {
        return menuService.removeMenu(id) ?
                Result.success() : Result.fail(ResultCode.DATA_DEL_ERROR);
    }

    /**
     * 分页查询菜单列表
     */
    @Operation(summary = "分页查询菜单列表", description = "根据条件分页查询菜单列表")
    @GetMapping("/page")
    public Result<IPage<MenuVO>> getPageList( MenuDTO menuDTO){
        return Result.success(menuService.getPageList(menuDTO));
    }
    /**
     * 查询菜单列表
     */
    @Operation(summary = "查询菜单列表", description = "查询所有菜单列表")
    @GetMapping("/list")
    public Result<List<MenuVO>> getMenuList(MenuDTO menuDTO) {
        return Result.success(menuService.getMenuList(menuDTO));
    }

    /**
     * 根据ID查询菜单详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "根据ID查询菜单详情", description = "根据ID查询菜单详情")
    public Result<MenuVO> getMenuById(@PathVariable Long id){
        return Result.success(menuService.getMenuDetailById(id));
    }



}
